package com.power.powercash;

import com.db4o.query.Predicate;
import com.power.utils.SearchEngine;
import com.power.utils.SearchText;

/**
 *
 * @author f6036477
 */
public class ConsultaCentroCustos extends Predicate<CentroCustos>
{

  private CentroCustos centro;

  private boolean empty;

	private SearchEngine engine;


  public ConsultaCentroCustos()
  {
    centro = null;
    empty = false;
		engine = new SearchEngine();
  }


  public ConsultaCentroCustos(CentroCustos c)
  {
    centro = c;
    empty = false;
  }


	public SearchEngine engine() {
		return engine;
	}


	public void setSearchEngine(SearchEngine eng) {
		engine = eng;
	}


  public CentroCustos getCentroCustos()
  {
    return centro;
  }


  public void setCentroCustos(CentroCustos c)
  {
    this.centro = c;
  }


  public void setEmptyQuery(boolean emptyQuery)
  {
    empty = emptyQuery;
  }


  public boolean isEmptyQuery()
  {
    return empty;
  }


  @Override
  public boolean match(CentroCustos c)
  {
    if(centro == null || c == null)
      return false;

    if(empty) return true;

    boolean nome = false, desc = false, nonull = false;

    if(centro.getName() != null
        && c.getName() != null
        && centro.getName().equals(c.getName())) {
      nome = true;
      nonull = true;
    } else if(centro.getName() == null)
      nome = true;

    if(centro.getDescricao() != null
        && c.getDescricao() != null) {

			if(engine == null) engine = new SearchEngine();

			SearchText src = new SearchText(c.getDescricao());
			SearchText sch = new SearchText(centro.getDescricao());
			engine.setSource(src);
			engine.setSearch(sch);

			desc = engine.match();
			nonull = true;
    } else if(centro.getDescricao() == null)
      desc = true;

    return nome && desc && nonull;
  }

}
